﻿using JewererBasic;
using JewererBasic.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.Entity;

namespace JewererBasic.Controllers
{
    public class TestController : Controller
    {
        MSQDBEntities db = new MSQDBEntities();

        PRODUCT pro =new PRODUCT();

        public ActionResult Index(string id=null)
        {            
            if (id != null)
            {
                var products = db.PRODUCTs.
                    Include(p => p.GAMMA).
                    Include(p => p.KIND).
                    Include(p => p.TYPE).
                    Include(p => p.UNIT).
                    Include(p => p.PRODUCT_COMPONENT).
                    Include(p => p.PRODUCT_PROCESS).
                    Include(p => p.PRODUCT_STONE);
                foreach (var item in products)
                {
                    if (item.Id == id)
                    {
                        pro = item;
                        break;
                    }
                }
            }            
            SetViewBag();
            return View(pro);
        }
        
        [HttpPost]
        public ActionResult Index(CastingDummy casting, 
            Product_Component_Dummy proCom, 
            Product_Stone_Dummy proStone, 
            PRODUCT_PROCESS proProcess,
            PRODUCT product, HttpPostedFileBase file)
        {                    
            // save product to database
            if (product.IdGamma>0
                &&product.IdKind>0
                &&product.IdType>0
                &&product.IdUnit>0                
                &&product.Size>0
                &&string.IsNullOrEmpty(product.Id) == false)
            {

                if (file != null && file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/Content"), fileName);
                    file.SaveAs(path);
                    product.Drawing = fileName;
                }

                var queryPro = db.PRODUCTs.Find(product.Id);
                if (queryPro != null)
                {
                    queryPro.IdGamma = product.IdGamma;
                    queryPro.IdKind = product.IdKind;
                    queryPro.IdType = product.IdType;
                    queryPro.IdUnit = product.IdUnit;
                    queryPro.MetalWeight = product.MetalWeight;
                    if(string.IsNullOrEmpty(product.Drawing)==false)
                        if(string.Equals(product.Drawing, queryPro.Drawing)==false)
                            queryPro.Drawing = product.Drawing;
                    queryPro.Size = product.Size;
                    db.SaveChanges();
                }
                else
                {    
                    // save to database
                    db.PRODUCTs.Add(product);
                    // create process
                    var processes = db.PROCESSes.ToList();
                    int i=1;
                    foreach (var item in processes)
                    {
                        db.PRODUCT_PROCESS.Add(new PRODUCT_PROCESS() { IdProcess = item.Id,IdProduct=product.Id, ProcessOrder=i});
                        ++i;
                    }                    
                    db.SaveChanges();                   
                }
            }
            else
            {
                SetViewBag();
                return RedirectToAction("Index");
            }

            if (proStone != null)
            {
                if (proStone.QtyS > 0
                    && proStone.IdStoneS != null
                    && proStone.WeightS > 0)
                {
                    proStone.IdProductS = product.Id;
                    var query = db.PRODUCT_STONE.Find(proStone.IdProductS, proStone.IdStoneS);
                    if (query != null)
                    {
                        db.PRODUCT_STONE.Remove(query);
                        db.SaveChanges();
                    }
                    PRODUCT_STONE pss = proStone.GetPRODUCT_STONE();
                    pss.STONE = db.STONEs.Find(pss.IdStone);
                    db.PRODUCT_STONE.Add(pss);
                    db.SaveChanges();
                }
            }            
            if (proCom != null)
            {
                if (proCom.IdComponentCom != null
                    && proCom.QtyCom > 0
                    && proCom.WeightCom > 0)
                {
                    proCom.IdProductCom = product.Id;
                    var query2 = db.PRODUCT_COMPONENT.Find(proCom.IdProductCom, proCom.IdComponentCom);
                    if (query2 != null)
                    {
                        db.PRODUCT_COMPONENT.Remove(query2);
                        db.SaveChanges();
                    }
                    PRODUCT_COMPONENT pccc = proCom.GetPRODUCT_COMPONENT();
                    pccc.COMPONENT = db.COMPONENTs.Find(pccc.IdComponent);
                    db.PRODUCT_COMPONENT.Add(pccc);
                    db.SaveChanges();
                }
            }
            if (casting != null)
            {
                if(casting.IdC!=null               
                    &&String.IsNullOrEmpty(casting.NameC)==false
                    &&String.IsNullOrEmpty(casting.DecriptionC)==false
                    &&String.IsNullOrEmpty(casting.MoldC)==false
                    && casting.WeightC > 0)
                {
                    //casting.IdProduct = product.Id;
                    //var query3 = db.CASTINGs.Find(casting.IdC);
                    //if (query3 != null)
                    //{
                    //    db.CASTINGs.Remove(query3);                        
                    //    db.SaveChanges();
                    //}                  
                    //db.CASTINGs.Add(casting.GetCASTING());
                    //db.SaveChanges();
                }                
            }
            if (proProcess != null)
            {
                if (proProcess.IdProcess > 0 && proProcess.Time > 0)
                {
                    proProcess.IdProduct = product.Id;
                    var query4 = db.PRODUCT_PROCESS.Find(proProcess.IdProduct, proProcess.IdProcess);
                    if (query4 != null)
                    {
                        db.PRODUCT_PROCESS.Remove(query4);
                        db.SaveChanges();
                    }
                    proProcess.PROCESS = db.PROCESSes.Find(proProcess.IdProcess);
                    db.PRODUCT_PROCESS.Add(proProcess);
                    db.SaveChanges();
                }                
            }
            
            this.SetViewBag();           
            var products = db.PRODUCTs.
                    Include(p => p.GAMMA).
                    Include(p => p.KIND).
                    Include(p => p.TYPE).
                    Include(p => p.UNIT).
                    Include(p => p.PRODUCT_COMPONENT).
                    Include(p => p.PRODUCT_PROCESS).
                    Include(p => p.PRODUCT_STONE);
            foreach (var item in products)
                if (item.Id == product.Id)
                    return View(item);
            return View(db.PRODUCTs.Find(product.Id));
        }       

        #region Edit and delete casting
        //public ActionResult EditCasting(string id = null)
        //{
        //    CASTING casting = db.CASTINGs.Find(id);
        //    if (casting == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    ViewBag.IdProduct = new SelectList(db.PRODUCTs, "Id", "Name", casting.IdProduct);
        //    ViewBag.IdType = new SelectList(db.TYPEs, "Id", "Name", casting.IdType);
        //    ViewBag.IdUnit = new SelectList(db.UNITs, "Id", "Name", casting.IdUnit);
        //    return View("EditCasting", casting);
        //}
        //[HttpPost]
        //public ActionResult EditCasting(CASTING casting)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Entry(casting).State = EntityState.Modified;
        //        db.SaveChanges();
        //    }
        //    ViewBag.IdProduct = new SelectList(db.PRODUCTs, "Id", "Name", casting.IdProduct);
        //    ViewBag.IdType = new SelectList(db.TYPEs, "Id", "Name", casting.IdType);
        //    ViewBag.IdUnit = new SelectList(db.UNITs, "Id", "Name", casting.IdUnit);
        //    return View("EditCasting", casting);
        //}

        //public ActionResult DeleteCasting(string id = null)
        //{
        //    CASTING casting = db.CASTINGs.Find(id);
        //    var idproduct = casting.IdProduct;
        //    if (casting != null)
        //    {
        //        db.CASTINGs.Remove(casting);
        //        db.SaveChanges();                
        //    }
        //    return RedirectToAction("Index", "Test", new { id = idproduct});
        //}      
        #endregion

        #region edit and delete component
        public ActionResult EditComponent(string id = null)
        {
            string[] ids = id.Split(',');
            PRODUCT_COMPONENT product_component = db.PRODUCT_COMPONENT.Find(ids[0], ids[1]);
            if (product_component == null)
            {
                return HttpNotFound();
            }
            ViewBag.IdComponent = new SelectList(db.COMPONENTs, "Id", "Name", product_component.IdComponent);
            ViewBag.IdProduct = new SelectList(db.PRODUCTs, "Id", "Name", product_component.IdProduct);
            return View("EditComponent", product_component);
        }
        [HttpPost]
        public ActionResult EditComponent(PRODUCT_COMPONENT product_component)
        {
            if (ModelState.IsValid)
            {
                db.Entry(product_component).State = EntityState.Modified;
                db.SaveChanges();
            }
            ViewBag.IdComponent = new SelectList(db.COMPONENTs, "Id", "Name", product_component.IdComponent);
            ViewBag.IdProduct = new SelectList(db.PRODUCTs, "Id", "Name", product_component.IdProduct);
            return View("EditComponent", product_component);
        }
        public ActionResult DeleteComponent(string id = null)
        {
            string[] ids = id.Split(',');
            PRODUCT_COMPONENT product_component = db.PRODUCT_COMPONENT.Find(ids[0], ids[1]);
            if (product_component != null)
            {
                db.PRODUCT_COMPONENT.Remove(product_component);
                db.SaveChanges();
            }
            return RedirectToAction("Index", "Test", new { id = ids[0]});
        }              
        #endregion

        #region edit delete stone
        public ActionResult EditStone(string id = null)
        {
            string[] ids = id.Split(',');
            PRODUCT_STONE product_stone = db.PRODUCT_STONE.Find(ids[0], ids[1]);
            if (product_stone == null)
            {
                return HttpNotFound();
            }
            ViewBag.IdProduct = new SelectList(db.PRODUCTs, "Id", "Name", product_stone.IdProduct);
            ViewBag.IdStone = new SelectList(db.STONEs, "Id", "Name", product_stone.IdStone);
            return View("EditStone", product_stone);
        }
        [HttpPost]
        public ActionResult EditStone(PRODUCT_STONE product_stone)
        {
            if (ModelState.IsValid)
            {
                db.Entry(product_stone).State = EntityState.Modified;
                db.SaveChanges();
            }
            ViewBag.IdProduct = new SelectList(db.PRODUCTs, "Id", "Name", product_stone.IdProduct);
            ViewBag.IdStone = new SelectList(db.STONEs, "Id", "Name", product_stone.IdStone);
            return View("EditStone", product_stone);
        }
        public ActionResult DeleteStone(string id = null)
        {
            string[] ids = id.Split(',');
            PRODUCT_STONE product_stone = db.PRODUCT_STONE.Find(ids[0], ids[1]);
            if (product_stone != null)
            {
                db.PRODUCT_STONE.Remove(product_stone);
                db.SaveChanges();                
            }
            return RedirectToAction("Index", "Test", new { id = ids[0] });
        }       
        #endregion

        #region process
        public ActionResult EditProcess(string id = null)
        {
            string[] ids = id.Split(',');
            PRODUCT_PROCESS product_process = db.PRODUCT_PROCESS.Find(ids[0], Int32.Parse(ids[1]));
            if (product_process == null)
            {
                return HttpNotFound();
            }
            return View("EditProductProcess", product_process);
        }
        [HttpPost]
        public ActionResult EditProcess(PRODUCT_PROCESS product_process)
        {
            if (ModelState.IsValid)
            {
                db.Entry(product_process).State = EntityState.Modified;
                db.SaveChanges();
            }
            return View("EditProductProcess", product_process);
        }
        public ActionResult DeleteProcess(string id = null)
        {
            string[] ids = id.Split(',');
            PRODUCT_PROCESS product_process = db.PRODUCT_PROCESS.Find(ids[0], Int32.Parse(ids[1]));
            if (product_process != null)
            {
                db.PRODUCT_PROCESS.Remove(product_process);
                db.SaveChanges();                
            }
            return RedirectToAction("Index", "Test", new { id = ids[0]});
        }       
        #endregion

        public void SetViewBag()
        {
            // for product
            ViewBag.IdKind = new SelectList(db.KINDs, "Id", "Name", pro.IdKind);
            ViewBag.IdType = new SelectList(db.TYPEs, "Id", "Name", pro.IdType);
            ViewBag.IdUnit = new SelectList(db.UNITs, "Id", "Name", pro.IdUnit);
            ViewBag.IdGamma = new SelectList(db.GAMMAs, "Id", "Name", pro.IdGamma);
            ViewBag.IdProduct = new SelectList(db.PRODUCTs, "Id", "Id", pro.Id);

            // for component partial view
            ViewBag.IdComponent = new SelectList(db.COMPONENTs, "Id", "Id");
           
            // for stone partial view
            ViewBag.IdStone = new SelectList(db.STONEs, "Id", "Name");
           
            // for process partial view
            ViewBag.IdProcess = new SelectList(db.PROCESSes, "Id", "Name");
        }
    }   
}
